package com.gxg.controller;

import com.gxg.entities.User;
import com.gxg.services.VNCService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class VNCController
{
  @Autowired
  private VNCService vncService;
  
  @RequestMapping({"/get_one_free_vnc"})
  @ResponseBody
  public String getOneFreeVNC(@RequestParam("screenSize") String screenSize, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    String result = this.vncService.getOneFreeExperimentalNode(request, screenSize);
    return result;
  }
  
  @RequestMapping({"/stop_vnc"})
  @ResponseBody
  public String stopVNC()
  {
    String result = this.vncService.stopVNC("192.168.1.101");
    return result;
  }
  
  @RequestMapping({"/keep_vnc_connection"})
  @ResponseBody
  public String keepVNCConnection(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    this.vncService.updateNodeTime(request);
    return "ok";
  }
  
  @RequestMapping({"/select_one_vnc_node"})
  @ResponseBody
  public String selectOneVNCNode(@RequestParam("ip") String ip, @RequestParam("screenSize") String screenSize, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    String result = this.vncService.selectVNCNode(ip, screenSize, request);
    return result;
  }
  
  @RequestMapping({"get_all_experimental_node"})
  @ResponseBody
  public String getAllExperimentalNode(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.vncService.getAllExperimentalNodeToJsonString();
  }
  
  @PostMapping({"/get_all_available_experimental_node"})
  @ResponseBody
  public String getAllAvailableExperimentalNode(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.vncService.getAllAvailableExperimentalNodeToJsonString(request);
  }
  
  @PostMapping({"/change_node_status"})
  @ResponseBody
  public String changeNodeStatus(@RequestParam("ip") String ip, @RequestParam("status") String status, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
      return this.vncService.changeNodeStatus(ip, status, user);
    }
    return "没有用户！";
  }
  
  @RequestMapping({"/get_node_exist"})
  @ResponseBody
  public String getNodeExist(HttpServletRequest request)
  {
    return this.vncService.getNodeExist(request);
  }
  
  @PostMapping({"/get_node_number_each_group"})
  @ResponseBody
  public String getNodeNumberEachGroup()
  {
    return this.vncService.getNodeNumberEachGroup();
  }
  
  @PostMapping({"/change_node_group"})
  @ResponseBody
  public String changeNodeGroup(@RequestParam("groupCount") String groupCount, HttpServletRequest request)
  {
    return this.vncService.changeNodeGroup(groupCount, request);
  }
}
